/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GISGraphGenerateBox.java
 *
 * Created on 2011-05-22, 16:52:55
 */
package gisgraph;

import edu.uci.ics.jung.graph.DirectedSparseGraph;
import gisgraph.components.*;
import org.jdesktop.application.Action;
import org.jgrapht.experimental.dag.DirectedAcyclicGraph;


/**
 *
 * @author Slawek
 */
public class GISGraphGenerateBox extends javax.swing.JDialog {

    private GISGraphView fView;
    
    /** Creates new form GISGraphGenerateBox */
    public GISGraphGenerateBox(java.awt.Frame parent, GISGraphView fView) {
        super(parent);
        initComponents();
        
        this.fView = fView;
    }
    
    @Action
    public void generate()
    {
        fView.setStatusText("Rozpoczęto generowanie grafu..");

        String algType = (String)cbAlgType.getSelectedItem();
        
        DirectedSparseGraph<GISVertex, GISEdge> graph;
        DirectedAcyclicGraph<GISVertex, JGraphEdge> dag;
        GISGraphGenerator generator = new GISGraphGenerator();

        int nrVertices = 0;
        int nrEdges = 0;
        
        try
        {
            nrVertices = Integer.parseInt(txtVertices.getText());
        }
        catch (NumberFormatException e)
        {
            lblError.setText("Niepoprawna ilość wierzchołków");
            return;
        }
        
        if (algType == "Algorytm 1") 
        {           
            try
            {
                nrEdges = Integer.parseInt(txtEdges.getText());
            }
            catch (NumberFormatException e)
            {
                lblError.setText("Niepoprawna ilość krawędzi");
                return;
            }
            
            try
            {
                dag = generator.create2(nrVertices, nrEdges, GISGraphApp.eFactory, GISGraphApp.vFactory);
            }
            catch (Exception ex)
            {
                lblError.setText("Podano nieprawidłowe parametry!");
                return;
            }
            graph = GraphConverter.ConvertToJung(dag);
        } 
        else 
        {
            try
            {
                graph = generator.create(nrVertices, GISGraphApp.eFactory, GISGraphApp.vFactory);
            }
            catch (Exception ex)
            {
                lblError.setText("Nieprawidłowe parametry!");
                return;
            }
        }
        
        
        lblError.setText("");
        this.setVisible(false);
        fView.LoadGraphLayout(graph);
        fView.setStatusText("Wygenerowano graf");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cbAlgType = new javax.swing.JComboBox();
        pnlAlg2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtEdges = new javax.swing.JTextField();
        txtVertices = new javax.swing.JTextField();
        lblError = new javax.swing.JLabel();
        btnGenerate = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gisgraph.GISGraphApp.class).getContext().getResourceMap(GISGraphGenerateBox.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jPanel1.setMaximumSize(new java.awt.Dimension(220, 165));
        jPanel1.setMinimumSize(new java.awt.Dimension(220, 165));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(220, 165));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cbAlgType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Algorytm 1", "Algorytm 2" }));
        cbAlgType.setName("cbAlgType"); // NOI18N
        cbAlgType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbAlgTypeItemStateChanged(evt);
            }
        });

        pnlAlg2.setName("pnlAlg2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtEdges.setText(resourceMap.getString("txtEdges.text")); // NOI18N
        txtEdges.setName("txtEdges"); // NOI18N

        javax.swing.GroupLayout pnlAlg2Layout = new javax.swing.GroupLayout(pnlAlg2);
        pnlAlg2.setLayout(pnlAlg2Layout);
        pnlAlg2Layout.setHorizontalGroup(
            pnlAlg2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAlg2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addGap(39, 39, 39)
                .addComponent(txtEdges, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))
        );
        pnlAlg2Layout.setVerticalGroup(
            pnlAlg2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAlg2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(txtEdges, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        txtVertices.setText(resourceMap.getString("txtVertices.text")); // NOI18N
        txtVertices.setName("txtVertices"); // NOI18N

        lblError.setForeground(resourceMap.getColor("lblError.foreground")); // NOI18N
        lblError.setText(resourceMap.getString("lblError.text")); // NOI18N
        lblError.setName("lblError"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gisgraph.GISGraphApp.class).getContext().getActionMap(GISGraphGenerateBox.class, this);
        btnGenerate.setAction(actionMap.get("generate")); // NOI18N
        btnGenerate.setText(resourceMap.getString("btnGenerate.text")); // NOI18N
        btnGenerate.setName("btnGenerate"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(pnlAlg2, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtVertices)
                                .addComponent(cbAlgType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(btnGenerate))
                .addGap(144, 144, 144))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(187, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbAlgType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtVertices, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlAlg2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(btnGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(jLabel4))
        );

        jLabel4.getAccessibleContext().setAccessibleName(resourceMap.getString("jLabel4.AccessibleContext.accessibleName")); // NOI18N

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbAlgTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbAlgTypeItemStateChanged

        if (evt.getStateChange() != 1)
        {
            return;
        }

        String selectedItem = (String)evt.getItem();
        
        if (selectedItem == "Algorytm 1") {
            pnlAlg2.setVisible(true);
        } else {
            txtEdges.setText("");
            pnlAlg2.setVisible(false);
        }
    }//GEN-LAST:event_cbAlgTypeItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGenerate;
    private javax.swing.JComboBox cbAlgType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblError;
    private javax.swing.JPanel pnlAlg2;
    private javax.swing.JTextField txtEdges;
    private javax.swing.JTextField txtVertices;
    // End of variables declaration//GEN-END:variables
}
